package com.stardata.liangshisq.tcslmon.util;

import java.util.Random;

public class CharUtil {

    /**
     * 获取随机字符串
     *
     * @param num 随机字符串的长度
     * @return String 随机字符串
     */
    public static String getRandomString(Integer num) {
        String base = "abcdefghijklmnopqrstuvwxyz0123456789@#$%^&*()!-+=~;:<>/,.?'";
        Random random = new Random();
        Random randomCase = new Random();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < num; i++) {
            int number = random.nextInt(base.length());
            int upperCase = randomCase.nextInt(2);
            char c = base.charAt(number);
            if (upperCase > 0) {
                sb.append((c+"").toUpperCase());
            }else {
                sb.append(c);
            }

        }
        return sb.toString();
    }

    /**
     * 获取随机字符串
     *
     * @param num 随机数字串长度
     * @return String 随机数字串
     */
    public static String getRandomNum(Integer num) {
        String base = "0123456789";
        Random random = new Random();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < num; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString();
    }

    /**
     * 右补位，左对齐
     *
     * @param oriStr   原字符串
     * @param len      目标字符串长度
     * @param fillChar 补位字符
     * @return 目标字符串
     */
    public static String padRight(String oriStr, int len, char fillChar) {
        int strlen = oriStr.length();
        StringBuilder sb = new StringBuilder();
        if (strlen < len) {
            for (int i = 0; i < len - strlen; i++) {
                sb.append(fillChar);
            }
        }
        return sb.toString() + oriStr;
    }

    /**
     * 左补位，右对齐
     *
     * @param oriStr   原字符串
     * @param len      目标字符串长度
     * @param fillChar 补位字符
     * @return 目标字符串
     */
    public static String padLeft(String oriStr, int len, char fillChar) {
        int strlen = oriStr.length();
        StringBuilder sb = new StringBuilder();
        if (strlen < len) {
            for (int i = 0; i < len - strlen; i++) {
                sb.append(fillChar);
            }
        }
        return oriStr + sb.toString();
    }
}
